<?php


namespace app\model\partner;


use app\model\cart\VerifyCartShapeModel;
use app\model\user\UserModel;
use think\Model;
use think\model\concern\SoftDelete;

class PartnerOrderModel extends Model
{
    protected $name = 'partner_order';
    protected $autoWriteTimestamp = true;

    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $append = ['pay_status_txt', 'reserve_time_txt', 'shape_id_txt'];

    protected $statusArr = [
        0 => '支付失败',
        1 => '待支付',
        2 => '待接单',
        3 => '已完成',
        4 => '已取消',
        5 => '已接单',
        6 => '拒绝接单',
    ];

    public function getStatusArr()
    {
        return $this->statusArr;
    }

    public function user()
    {
        return $this->hasOne(UserModel::class, 'id', 'user_id')->bind(['user_mobile' => 'mobile', 'user_name' => 'username']);
    }

    public function coach()
    {
        return $this->hasOne(UserPartnerModel::class, 'id', 'coach_user_id')->bind(['coach_mobile' => 'mobile',
            'coach_username' => 'username', 'coach_avatar' => 'avatar']);
    }

    public function getPayStatusTxtAttr($value, $data)
    {
        if (!isset($data['status'])) {
            return '';
        }
        return isset($this->statusArr[$data['status']]) ? $this->statusArr[$data['status']] : '';
    }

    public function getReserveTimeTxtAttr($value, $data)
    {
        if (!isset($data['reserve_time'])) {
            return '';
        }
        return date('Y-m-d H:i:s', $data['reserve_time']);
    }

    public function getShapeIdTxtAttr($value, $data)
    {
        if (!isset($data['shape_id'])) {
            return [];
        }
        return VerifyCartShapeModel::where('id', $data['shape_id'])->value('title');
    }
}